﻿// Asio server session.
// Author: Jin Qing (http://blog.csdn.net/jq0123)

#ifndef SVR_COMMON_ASIO_ASIO_SVR_SESSION_H
#define SVR_COMMON_ASIO_ASIO_SVR_SESSION_H

#include <memory>  // for enable_shared_from_this<>

#include "asio_session_base.h"  // for CAsioSessionBase
#include "rpc/rpc_req_resp_fwd.h"  // for RpcResponse

class CClientMailBox;

// Server accepted session. The remote peer is client or server.
class CAsioSvrSession :
	public std::enable_shared_from_this<CAsioSvrSession>,
	public CAsioSessionBase
{
public:
	explicit CAsioSvrSession(CAsioServer& rServer);
	virtual ~CAsioSvrSession();

public:
	socket& GetSocket()
	{
		return m_socket;
	}
	void Disconnect();
	CClientMailBox& GetCltMailBox()
	{
		assert(m_pCltMailBox);
		return *m_pCltMailBox;
	}

public:
	void Go();

private:
	void LoopRead(boost::asio::yield_context yield);
	void LoopWrite(boost::asio::yield_context yield);

private:
	void HandleData(const char* pData, uint32_t uLen);
	void HandleResponse(const rpc::RpcResponse& response);

private:
	CMailBox& GetMailBox() override;
	const CMailBox& GetMailBox() const override;

private:
	socket m_socket;
	std::unique_ptr<CClientMailBox> m_pCltMailBox;
};

#endif
